package book.BouncySquare;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

class SquareRenderer implements GLSurfaceView.Renderer
{
    public SquareRenderer(boolean useTranslucentBackground)
    {
	this.setmTranslucentBackground(useTranslucentBackground);
	mSquare = new Square(); // 3
	mAngle = 0.0f;
	mTransZ = -3.0f;
	mTargetZ = mTransZ;
    }

    public void onDrawFrame(GL10 gl) // 4
    {
	if (mAngle != mTargetAngle)
	{
	    mAngle += (mTargetAngle - mAngle) / 20;
	}
	
	if (mTransZ != mTargetZ)
	{
	    mTransZ += (mTargetZ - mTransZ) / 20;
	}
	

	gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // 5
	gl.glMatrixMode(GL10.GL_MODELVIEW); // 6
	gl.glLoadIdentity(); // 7
	gl.glTranslatef(0, (float) Math.sin(mTransY), mTransZ); // 8
	gl.glRotatef(mAngle, 0, -1.0f, 0);
	gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // 9
	gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

	gl.glEnable(GL10.GL_CULL_FACE);
	gl.glCullFace(GL10.GL_BACK);
	mSquare.draw(gl); // 10

	gl.glCullFace(GL10.GL_FRONT);
	mSquare.draw(gl); // 10

	mTransY += .075f;
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) // 11
    {
	gl.glViewport(0, 0, width, height); // 12
	float ratio = (float) width / height;
	gl.glMatrixMode(GL10.GL_PROJECTION); // 13
	gl.glLoadIdentity();
	gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); // 14
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) // 15
    {
	gl.glDisable(GL10.GL_DITHER); // 16
	gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // 17
		GL10.GL_FASTEST);
	if (ismTranslucentBackground()) // 18
	{
	    gl.glClearColor(0, 0, 0, 0);
	}
	else
	{

	    gl.glClearColor(1, 1, 1, 1);
	}
	gl.glEnable(GL10.GL_CULL_FACE); // 19
	gl.glShadeModel(GL10.GL_SMOOTH); // 20
	gl.glEnable(GL10.GL_DEPTH_TEST); // 21
    }

    public boolean ismTranslucentBackground()
    {
	return mTranslucentBackground;
    }

    public void setmTranslucentBackground(boolean mTranslucentBackground)
    {
	this.mTranslucentBackground = mTranslucentBackground;
    }

    public float getmTransX()
    {
	return mTransX;
    }

    public void setmTransX(float mTransX)
    {
	this.mTransX = mTransX;
    }

    public float getmAngle()
    {
	return mAngle;
    }

    public void setmAngle(float mAngle)
    {
	this.mAngle = mAngle;
    }

    public float getmTransZ()
    {
	return mTransZ;
    }

    public void setmTransZ(float mTransZ)
    {
	this.mTransZ = mTransZ;
    }

    public float getmTargetAngle()
    {
	return mTargetAngle;
    }

    public void setmTargetAngle(float mTargetAngle)
    {
	this.mTargetAngle = mTargetAngle;
    }

    public float getmTargetZ()
    {
	return mTargetZ;
    }

    public void setmTargetZ(float mTargetZ)
    {
	this.mTargetZ = mTargetZ;
    }

    private boolean mTranslucentBackground;

    private Square mSquare;

    private float mTransY;

    private float mTransX;

    private float mAngle;

    private float mTransZ;

    private float mTargetAngle;
    
    private float mTargetZ;
}
